package com.karaku.karaku_collector.util;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Created by guoping on 2017/9/20.
 */

public class ClassUtil {
    public static boolean isSubClass(Class subClass, Class parentClass) {
        if (subClass == null || parentClass == null) {
            return false;
        }
        if (subClass.equals(parentClass)) {
            return true;
        }
        if (isSubClass(subClass.getSuperclass(), parentClass)) {
            return true;
        }
        Class[] interfaces = subClass.getInterfaces();
        for (Class i:interfaces) {
            if (isSubClass(i, parentClass)) {
                return true;
            }
        }
        return false;
    }

    public static ParameterizedType parameterizedType(final Class raw, final Type... args) {
        return new ParameterizedType() {
            public Type getRawType() {
                return raw;
            }

            public Type[] getActualTypeArguments() {
                return args;
            }

            public Type getOwnerType() {
                return null;
            }
        };
    }
}
